home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / audio_sgi.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  5KB  |  216 lines

  1. /*
  2.  
  3.      Silicon Graphics audio drivers adapted from the stand-alone
  4.       Speak Freely for SGI designed and implemented by:
  5.  
  6.                 Paul Schurman
  7.                 Espoo, Finland
  8.  
  9.                  16 July 1995
  10.  
  11. */
  12.  
  13. #include "speakfree.h"
  14.  
  15. #ifdef sgi
  16. #include <fcntl.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <dmedia/audio.h>
  20.  
  21. #include "ulaw2linear.h"
  22.  
  23. /*  Local variables  */
  24.  
  25. static ALport audioport;          /* Audio I/O port */
  26. static long origParams[] = {
  27.     AL_INPUT_RATE, 0,
  28.     AL_OUTPUT_RATE, 0,
  29.     AL_LEFT_SPEAKER_GAIN, 0,
  30.     AL_RIGHT_SPEAKER_GAIN, 0,
  31.     AL_LEFT_INPUT_ATTEN, 0,
  32.     AL_RIGHT_INPUT_ATTEN, 0
  33. };
  34. static long origMute[] = {
  35.     AL_SPEAKER_MUTE_CTL, 0
  36. };
  37.  
  38. /*  SOUNDINIT  --  Open the sound peripheral and initialise for
  39.            access.  Return TRUE if successful, FALSE
  40.            otherwise.  */
  41.  
  42. int soundinit(iomode)
  43.   int iomode;
  44. {
  45.     ALconfig audioconfig; 
  46.     int err;
  47.     static long params[] = {
  48.     AL_INPUT_RATE, 8000,
  49.     AL_OUTPUT_RATE, 8000
  50.     };
  51.  
  52.     ALseterrorhandler(0);
  53.  
  54.     /* Get a new audioconfig and set parameters. */
  55.   
  56.     audioconfig = ALnewconfig();   
  57.     ALsetsampfmt(audioconfig, AL_SAMPFMT_TWOSCOMP);
  58.     ALsetwidth(audioconfig, AL_SAMPLE_16);
  59.     ALsetqueuesize(audioconfig, 8000L);  
  60.     ALsetchannels(audioconfig, AL_MONO);   
  61.  
  62.     /* Save original of all global modes we change. */ 
  63.  
  64.     ALgetparams(AL_DEFAULT_DEVICE, origMute, 2);
  65.     ALgetparams(AL_DEFAULT_DEVICE, origParams, 12);
  66.  
  67.     /* Set input and output data rates to 8000 samples/second. */
  68.  
  69.     ALsetparams(AL_DEFAULT_DEVICE, params, 4);
  70.  
  71.     /* Open the audioport. */
  72.  
  73.     audioport = ALopenport((iomode & O_WRONLY) ? "Speaker" : "Mike",
  74.                            (iomode & O_WRONLY) ? "w" : "r", audioconfig);
  75.     if (audioport == (ALport) 0) {    
  76.        err = oserror();      
  77.        if (err == AL_BAD_NO_PORTS) {
  78.           fprintf(stderr, " System is out of audio ports\n"); 
  79.        } else if (err == AL_BAD_DEVICE_ACCESS) { 
  80.           fprintf(stderr, " Couldn't access audio device\n"); 
  81.        } else if (err == AL_BAD_OUT_OF_MEM) { 
  82.           fprintf(stderr, " Out of memory\n"); 
  83.        } 
  84.        return FALSE;
  85.     }  
  86.  
  87.     if (ALfreeconfig(audioconfig) != 0) {
  88.        err = oserror();
  89.        if (err == AL_BAD_CONFIG) {
  90.           fprintf(stderr, " Config not valid");
  91.       return FALSE;
  92.        }
  93.     } 
  94.  
  95.     /* Initialized the audioport okay. */
  96.  
  97.     return TRUE;
  98. }
  99.  
  100. /*  SOUNDTERM  --  Close the sound device.  */
  101.  
  102. void soundterm()
  103. {
  104.     ALsetparams(AL_DEFAULT_DEVICE, origParams, 12);
  105.     ALsetparams(AL_DEFAULT_DEVICE, origMute, 2);
  106.     ALcloseport(audioport);
  107. }
  108.  
  109. /*  SOUNDPLAY  --  Begin playing a sound.  */
  110.  
  111. void soundplay(len, buf)
  112.   int len;
  113.   unsigned char *buf;
  114. {
  115.     int i;
  116.     short abuf[BUFL];
  117.  
  118.     for (i = 0; i < len; i++) {
  119.     abuf[i] = audio_u2s(buf[i]);
  120.     }
  121.     ALwritesamps(audioport, abuf, len);
  122. }
  123.  
  124. /*  SOUNDPLAYVOL  --  Set playback volume from 0 (silence) to 100 (full on). */
  125.  
  126. void soundplayvol(value)
  127.   int value;
  128. {
  129.     long par[] = {
  130.     AL_LEFT_SPEAKER_GAIN, 0,
  131.     AL_RIGHT_SPEAKER_GAIN, 0
  132.     };
  133.  
  134.     par[1] = par[3] = (value * 255L) / 100;
  135.     ALsetparams(AL_DEFAULT_DEVICE, par, 4);
  136. }
  137.  
  138. /*  SOUNDRECGAIN  --  Set recording gain from 0 (minimum) to 100 (maximum).  */
  139.  
  140. void soundrecgain(value)
  141.   int value;
  142. {
  143.     long par[] = {
  144.     AL_LEFT_INPUT_ATTEN, 0,
  145.     AL_RIGHT_INPUT_ATTEN, 0
  146.     };
  147.  
  148.     par[1] = par[3] = ((100 - value) * 255L) / 100;
  149.     ALsetparams(AL_DEFAULT_DEVICE, par, 4);
  150. }
  151.  
  152. /*  SOUNDDEST  --  Set destination for generated sound.  If "where"
  153.            is 0, sound goes to the built-in speaker; if
  154.            1, to the audio output jack. */
  155.  
  156. void sounddest(where)
  157.   int where;
  158. {
  159.     /* Since we can't mute independently, and this is used only
  160.        for ring, always unmute the speaker and headphones. */
  161.  
  162.     static long par[] = {
  163.     AL_SPEAKER_MUTE_CTL, AL_SPEAKER_MUTE_OFF
  164.     };
  165.  
  166.     ALsetparams(AL_DEFAULT_DEVICE, par, 2);
  167. }
  168.  
  169. /*  SOUNDGRAB  --  Return audio information in the record queue.  */
  170.  
  171. int soundgrab(buf, len)
  172.     char *buf;
  173.     int len;
  174. {
  175.     int i;
  176.     short sb[BUFL];
  177. /*  long filled = ALgetfilled(audioport);
  178.  
  179.     if (len > filled) {
  180.     len = filled;
  181.     }
  182. len=488;
  183. */
  184. /*len=1600;*/
  185.     if (len > 0) {
  186.        ALreadsamps(audioport, sb, len);
  187.  
  188.       
  189.        for (i = 0; i < len; i++) {
  190.       buf[i] = audio_s2u(sb[i]);
  191.        }
  192.     }
  193.     return len;
  194. }
  195.  
  196. /*  SOUNDFLUSH    --  Flush any queued sound.  */
  197.  
  198. void soundflush()
  199. {
  200.     short sb[BUFL];
  201.  
  202.     while (TRUE) {
  203.     long l = ALgetfilled(audioport);
  204.  
  205.     if (l < 400) {
  206.         break;
  207.     } else {
  208.         if (l > BUFL) {
  209.         l = BUFL;
  210.         }
  211.         ALreadsamps(audioport, sb, l);
  212.     }
  213.     }
  214. }
  215. #endif /* sgi */
  216.